package com.example.demo.controller;

import com.example.demo.entity.CommonResult;
import com.example.demo.entity.drugs;
import com.example.demo.service.DrugService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
//@CrossOrigin
@Slf4j
public class DrugsController {


        @Autowired
    DrugService drugService;
        @ResponseBody
        @GetMapping("/Drugs/get_drugs_Onepage")
        @ApiImplicitParams({ @ApiImplicitParam(name = "pageNum", value = "第几页，不传递默认第一页", dataType = "Integer", paramType = "query"), @ApiImplicitParam(name = "pageSize", value = "一页的数据量，默认10条数据 ",  dataType = "Integer", paramType = "query")})
        @ApiOperation(notes = "药品管理", value = "get_drugs_Onepage")
        public CommonResult getOnepage( @RequestParam (value ="pageNum", defaultValue = "1")Integer pageNum,
                                        @RequestParam(value ="pageSize",defaultValue = "10") Integer pageSize){

            CommonResult commonResult = drugService.GetOnepagesdrug(pageNum, pageSize);

            return commonResult;
        }


    @ResponseBody
    @PostMapping("/Drugs/Adddrugs")
    @ApiOperation(notes = "药品添加", value = "adddrugs")
    public CommonResult Adddrugs( @ApiParam(name = "药品信息",required = true) @RequestBody drugs drugs){
        if (drugs.getRole().equals("1")){
            CommonResult adddrugs = drugService.Adddrugs(drugs);
            return adddrugs;
        }
        return new CommonResult(201,"药品添加失败,权限不够");
    }



    @ResponseBody
    @PostMapping("/Drugs/Updatedrugs")
    @ApiOperation(notes = "药品修改(药品ID唯一)", value = "Updatedrugs")
    public CommonResult Updatedrugs( @ApiParam(name = "药品信息",required = true) @RequestBody drugs drugs){
        String role = drugs.getRole();
        if(!role.equals("1")){
            return new CommonResult(201,"您无法更新失败,权限不够");
        }
        CommonResult updatedrugs = drugService.Updatedrugs(drugs);
        return updatedrugs;
    }

    @ResponseBody
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "主键id"),
            @ApiImplicitParam(name = "drug_id",value = "药品id")}
    )
    @GetMapping("/Drugs/deletedrugs")
    public CommonResult deletedrugs(Integer id,  String drug_id,String role) {
       if (!role.equals("1")){
           return new CommonResult(201,"您无法删除,权限不够");
       }
        CommonResult deletedrugs = drugService.deletedrugs(id, drug_id);
        return deletedrugs;
    }


    @ResponseBody
    @ApiImplicitParams({
            @ApiImplicitParam(name = "drug_name",value = "药品名字"),
            @ApiImplicitParam(name = "drug_id",value = "药品id")}
    )
    @GetMapping("/Drugs/selectdrugs")
    @ApiOperation(notes = "更ID和名字查询", value = "selectdrugs")
    public CommonResult selectdrugs(String drug_id,  String drug_name) {
        CommonResult deletedrugs = drugService.selectdrugs(drug_id,drug_name);
        return deletedrugs;
    }

}
